<template>
    <view class="cash-out-record pd-v-24 pd-b-40">
        <view class="cash-out-audit__header">
            <u--form
                labelWidth="80"
                labelPosition="left"
                :model="form"
            >
                <view class="border8 pd-l-24 bg-white mr-t-20">
                    <u-form-item
                        label="时间"
                        prop="timeRange"
                        borderBottom
                        @click="showDatePicker"
                    >
                        <u--input
                            v-model.trim="form.timeRange"
                            border="none"
                            :customStyle="{padding:'12rpx 0'}"
                            placeholder="请选择日期"
                            suffixIcon="calendar"
                            readonly
                            :suffixIconStyle="{marginRight:'12rpx',fontSize:'50rpx'}"
                        ></u--input>
                    </u-form-item>
                </view>
            </u--form>
        </view>
        <view class="pd-v-24 pd-h-32 bg-white border8 mr-t-12 flex flex-center" v-for="(item,index) in list" :key="index+'index'">
            <view class="flex-1">
                <view class="flex flex-center">
                    <view class="flex-1 font32 lh-44 bold color-default">
                      {{item.withdrawalUserName}} 
                    </view>
                    <view class="font26 lh-36 color-main" :class='{"color-red": item.status == "2" ? true : false, "color-green" : (item.status == "1"  || item.status == "3") ? true : false, "color-yellow" : item.status == "0" ? true : false }'>
                        {{item.status == 0 ? '待审核' : item.status == '1' ? '通过' : item.status == '2' ? '驳回' : item.status == '3' ? '已打款' : ''}}
                    </view>
                </view>

                <view class="flex flex-center mr-t-16">
                    <view class="flex-1 font26 lh-36 color-note">{{item.createTime}}</view>
                    <view class="font26 lh-36 color-default bold">¥{{item.withdrawalPrice}}</view>
                </view>
                
                <view v-if="item.status == '1'" class="pd-t-20">
                    <u-button type="primary" shape="circle" text="点击提现" size="small" @click="cashOut(item)" :customStyle="{width:'200rpx'}"></u-button>
                </view>
                
                
            </view>

           
        </view>
            
        <u-calendar 
            mode="range"
            :defaultDate="calendar.defaultDate"
            :maxDate="calendar.maxDate"
            :minDate="calendar.minDate"
            :monthNum="calendar.monthNum" 
            :show="show" @close="show = false;" @confirm="timeConfirm"></u-calendar>
            
    </view>
</template>

<script setup>
import { useStore } from '@/store/index.js';
import { ref, reactive, onMounted, nextTick } from 'vue';
import { onLoad, onShow,onReachBottom } from '@dcloudio/uni-app';
import { withdrawalList,getTransferDetailByOutNo, withdrawal } from '@/api/proxy.js'


const list = ref([])
const total = ref(0)
const params = ref({
    pageNum: 1,
    pageSize: 10
})
const show = ref(false);
const calendar = ref({
    minDate: '',
    maxDate: '',
    defaultDate: '',
    monthNum: 13,
})

const form = ref({
    timeRange: '',
    searchStartTime:'',
    searchEndTime:'',
});

onShow(() => {
    
})

onLoad(() => {
    getWithdrawalList()
    chooseTimed()
})

onReachBottom(() => {
    if(list.value.length < total.value) {
        params.value.pageNum++;
        getWithdrawalList()
    }

})


const showDatePicker = () => {
    console.log('showDatePicker');
    show.value = true;
};
const timeConfirm = (e) => {
    console.log('confirm', e);
    form.value.timeRange = e[0] +'~'+ e[e.length-1];
    form.value.searchStartTime = e[0] + ' 00:00:00';
    form.value.searchEndTime = e[e.length-1] + ' 23:59:59';
    show.value = false;
    list.value = []
    params.value.pageNum = 1
    getWithdrawalList()
};

const chooseTimed = () => {
    let date = new Date();
    let year = date.getFullYear();
    let month = date.getMonth() + 1;
    let day = date.getDate();
    if (month <= 9) {
    month = '0' + month;
    }
    if (day <= 9) {
    day = '0' + day;
    }
    let minyear = year - 1;
    calendar.value.minDate = minyear + '-' + month + '-' + day;
    calendar.value.maxDate = year + '-' + month + '-' + day;
    calendar.value.defaultDate = year + '-' + month + '-' + day;
}
const getWithdrawalList = () => {
    let param = {...params.value,...form.value}
   
    withdrawalList({...param}).then(res => {
        if(res.code == 200) {
            list.value = [...list.value,...res.rows]
            total.value = res.total; 
        }
    })
}


const getGetTransferDetailByOutNo = (id) => {
    return new Promise((resolve, reject) => {
        getTransferDetailByOutNo({outBillNo:id}).then(res => {
            resolve(res)
        })
    })
}

const getWithdrawal = (id) => {
    return new Promise((resolve, reject) => {
        withdrawal({
            withdrawalId:id
        }).then(res => {
            resolve(res)
        })
    })
}

const cashOut = (item) => {
    uni.showModal({
        title: '提示',
        content: '是否确认提现？',
        success: async (res) => {
            if (res.confirm) {
                try {
                    uni.showLoading({
                        title: '提现中...'
                    })
                    // let transferDetail = await getGetTransferDetailByOutNo(item.id)
                    // if(transferDetail.code == 200) {
                    //     if(transferDetail.data.state == 'WAIT_USER_CONFIRM') {
                            let withdrawalDetail = await getWithdrawal(item.id)
                            if(withdrawalDetail.code == 200) {
                                uni.requestMerchantTransfer({
                                    mchId: '1709428836',
                                    package: withdrawalDetail.data.packageInfo,
                                    appId:'wx3813ea04a0196aea',
                                    success: function (res) {
                                        uni.hideLoading()
                                        console.log(res,'resssssssssssss')
                                        uni.showToast({
                                            title: '提现成功',
                                            icon: 'none'
                                        })
                                    },
                                    fail: function (err) {
                                        uni.hideLoading()
                                        console.log(err,'errrrrrrrrrrrr') 
                                    }
                                })
                            } else {
                                uni.hideLoading()
                                uni.showToast({
                                    title: withdrawalDetail.msg,
                                    icon: 'none'
                                })
                            }
                    //     }
                    // } else {
                    //     uni.hideLoading()
                    //     uni.showToast({
                    //         title: transferDetail.msg,
                    //         icon: 'none'
                    //     })
                    // }
                }catch(err) {
                    uni.hideLoading()
                    uni.showToast({
                        title: '提现失败',
                        icon: 'none'
                    })
                    console.log(err,'eeeeeeeeeee')
                }
            }
        }
    })
    
}





</script>

<style scoped lang="scss">
.cash-out-record {
    background:#F7F7F7;
    min-height:100vh;

    
    
}
</style>
